package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.resource.date.DateUtils;
import com.itheima.health.service.OrderSettingService;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderSettingSrviceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {


    @Override
    public void bachOrderSetting(List<String[]> list) {
        //获取到方法获取到的数据
        List<OrderSetting> orderSettings = StrignToList(list);
        ArrayList<OrderSetting> orderSettingSave = new ArrayList<>();
        ArrayList<OrderSetting> orderSettingUpdate = new ArrayList<>();

        //循环遍历orderSettings集合中查到磁盘中的Xlsx数据
        for (OrderSetting orderSetting : orderSettings) {
            //获取数据中的时间对象
            Date orderDate = orderSetting.getOrderDate();

            //封装，查询，调用的时候用此集合，
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
            //将orderDate对象中的数据封装到此集合中
            queryWrapper.eq("ORDERDATE",orderDate);
            //用BaseMapper方法查询其中有没有这样的数据
            OrderSetting orderSettingisExist = baseMapper.selectOne(queryWrapper);

            //如果有就用更新，如果没有就用保存
            if (orderSettingisExist!=null){
                orderSetting.setId(orderSettingisExist.getId());//查询id封装到orserSetting对象到当中
                orderSettingUpdate.add(orderSetting);
            }else{
                orderSettingSave.add(orderSetting);
            }
        }
        saveBatch(orderSettingSave);
        updateBatchById(orderSettingUpdate);
    }

    @Override
    public List<Map> findOrderSettingsData(String yearAndMonth) {
        String  beginDate  = yearAndMonth+"-1";
        String  endDate  = yearAndMonth+"-31";

        List<Map> mapList =  baseMapper.findOrderSettingsData(beginDate,endDate);

        return mapList;
    }

    @Override
    public void updateOrderSettingData(String dateparam, Integer num) {
        baseMapper.updateOrderSettingData(dateparam,num);
    }
    //    @Override
//    public List<Map> findOrderSettingsData(String yearAndMonth) {
//
//        List<Map>  mapList = new ArrayList<Map>();
//        //   2020-8      1       31 最后一天   select  * from  xxx where  orderdate between  2020-8-1  and  2020-8-31
//        String  beginDate  = yearAndMonth+"-1";
//        String  endDate  = yearAndMonth+"-31";
//        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<OrderSetting>();
//        queryWrapper.between("ORDERDATE",beginDate,endDate);
//        List<OrderSetting> listOrderSettings = list(queryWrapper);
//        for (OrderSetting orderSetting : listOrderSettings) {
//            //   {date: 1   ,number  : 200   ,xxx :20}  Map
//            Date orderDate = orderSetting.getOrderDate();  //  2020-8-12
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(orderDate);//   当前日期
//            //  获取指定日期   对应 几号
//            int day = calendar.get(Calendar.DAY_OF_MONTH);
////           int day =  orderDate.getDate();
//            Map  map = new HashMap();
//            map.put("date",day);
//            map.put("number",orderSetting.getNumber());
//            map.put("reservations",orderSetting.getReservations());
//            mapList.add(map);
//
//        }
//
//        return mapList;
//    }

    private List<OrderSetting> StrignToList(List<String[]> list) {

        //如果list中有数据就遍历它，将他封装到实体类担当中，并将其保存到新的集合中
        if (list != null && list.size() != 0) {
            List<OrderSetting> orderSettings = new ArrayList<>();
            for (String[] strings : list) {
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                Date date = DateUtils.parseString2Date(strings[0], "yyyy/MM/dd");
                orderSetting.setOrderDate(date);
                orderSetting.setReservations(0);
                orderSettings.add(orderSetting);
            }
            return orderSettings;
        } else {
            return null;
        }
    }
}
